package puzzle.projecteuler.p100;

import astudy.util.Permutation;

public class Problem043 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		long sum = 0;
		int[] c = {0,1,2,3,4,5,6,7,8,9};
		while (c != null) {
			if (check(c)) {
				String n = "";
				for (int i: c) {
					n += i;
				}
				System.out.println(n);
				sum += Long.valueOf(n);
			}
			c = Permutation.next(c);
		}
		System.out.println(sum);
	}

	private static boolean check(int[] c) {
		return (div(c[1],c[2],c[3],2) && div(c[2],c[3],c[4],3) &&
				div(c[3],c[4],c[5],5) && div(c[4],c[5],c[6],7) &&
				div(c[5],c[6],c[7],11) && div(c[6],c[7],c[8],13) &&
				div(c[7],c[8],c[9],17));
	}

	private static boolean div(int d1, int d2, int d3, int d) {
		return ((d1*100 + d2*10 + d3)%d == 0); 
	}
}
